
#### Packages that are needed ####
packagesUsed <- c("tidyverse","cem", "MatchIt", "cobalt",
                  "broom", "ggpubr", "margins", "stargazer", "texreg")
sapply(packagesUsed, library, character.only = TRUE)   

#### loading dataset used for the judgment outcome analysis ####

load("judgments.Rda")

#### Selecting data for analysis #####
df <- filter(judgments, 
             Importance.Level != "3", # We exclude cases of importance level 3 because these merely apply existing case law)
             violationOrNoViolation == 1, # We only include judgments that decide on whether a violation has occured
             Judgment.Date >= as.Date("1998-11-01", format = "%Y-%m-%d"))

#### Matching and regression models ####
## Model 1 (Coarsened exact matching. Consolidated democracies vs. other states)
matchingData1 <- na.omit(df[,c( "ConDem", "ConsolidatedChar",
                                "year",
                                "BacklashTimevarying",
                                "post2005", 
                                "post2010",
                                "UK",
                                "respondent_1",
                                "proportionCasesClosed",
                                "shareViolationRulings",
                                "GrandChamberJudgment" ,"article2dummy",
                                "article3dummy" ,"article5dummy" , 
                                "article6dummy","article8dummy" , 
                                "article10dummy","article14dummy" ,
                                "protocol1_1dummy", "Importance1",
                                "Importance2",
                                "ViolationRuling", 
                                "medianJudge",
                                "lawyerPreviousViolationJudgments",
                                "EUmember2018",
                                "refugee",  "prisoner", 
                                "cowcode")])

cutPoints <- list(medianJudge = 4, 
                  proportionCasesClosed = c(0,0.25,0.5, 0.75, 1), 
                  lawyerPreviousViolationJudgments = c(0,1, 10), 
                  year = 10)

m.cem1 <- matchit(ConDem ~ year +
                    GrandChamberJudgment +
                    proportionCasesClosed +
                    article2dummy + article3dummy +
                    article5dummy + article6dummy+
                    article8dummy + article10dummy +
                    article14dummy + protocol1_1dummy +
                    Importance1 + Importance2 +
                    medianJudge +
                    lawyerPreviousViolationJudgments +
                    refugee + prisoner,
                  data = matchingData1, 
                  method="cem", cutpoints = cutPoints)
summary(m.cem1)

matchingData1$weight <- m.cem1$weights
matchedCem1  <- matchingData1[which(matchingData1$weight != 0),]

matchedBacklash <- filter(matchingData1, BacklashTimevarying == 1)
matchedNonBacklash <- filter(matchingData1, BacklashTimevarying == 0)


balanceVarLabels <- data.frame(old =  c( "proportionCasesClosed" ,
                                         "GrandChamberJudgment",
                                         "article2dummy" ,
                                         "article3dummy",
                                         "article5dummy",
                                         "article6dummy",
                                         "article8dummy",
                                         "article10dummy",
                                         "article14dummy",
                                         "protocol1_1dummy",
                                         "Importance1"  ,
                                         "Importance2",
                                         "year",
                                         "medianJudge",
                                         "lawyerPreviousViolationJudgments",
                                         "refugee",
                                         "prisoner"),
                               new = c("Proportion of judgments\n complied with in previous year",
                                       "Grand Chamber judgment",
                                       "Article 2 case",
                                       "Article 3 case",
                                       "Article 5 case", 
                                       "Article 6 case",
                                       "Article 8 case", 
                                       "Article 10 case",
                                       "Article 14 case",
                                       "Protocol 1, article 1 case",
                                       "Importance level 1",
                                       "Importance level 2",
                                       "Year of judgment",
                                       "Ideal point, median judge",
                                       "Previous experience of legal council",
                                       "Refugee or immigrant applicant",
                                       "Prisoner applicant"))

BacklashBalance <- love.plot(bal.tab(dplyr::select(matchedBacklash,
                                                   -ConDem, -ConsolidatedChar,
                                                   -shareViolationRulings, -post2005,
                                                   -post2010, - UK, -weight, -EUmember2018, - ViolationRuling, -cowcode, -respondent_1),
                                     continuous = "std", 
                                     binary = "std",
                                     treat = matchedBacklash$ConDem, 
                                     method = "matching",
                                     weights = matchedBacklash$weight,
                                     estimand = "ATT"),
                             drop.distance = TRUE,
                             var.names = balanceVarLabels,
                             #  var.order = "unadjusted", 
                             colors = c("black","grey"), 
                             abs = FALSE,
                             shapes = c(17,19))+
  theme_classic()+
  theme(axis.text=element_text(size=5))+
  ggtitle("Expressing Public Criticism")+
  xlim(-1,1)

NonBacklashBalance <- love.plot(bal.tab(dplyr::select(matchedNonBacklash, 
                                                      -ConDem, -ConsolidatedChar,-shareViolationRulings, -post2005,-post2010, - UK, -weight, -EUmember2018, - ViolationRuling, -cowcode, -respondent_1),
                                        continuous = "std", 
                                        binary = "std",
                                        treat = matchedNonBacklash$ConDem, 
                                        method = "matching",
                                        weights = matchedNonBacklash$weight,
                                        estimand = "ATT"),
                                drop.distance = TRUE,
                                var.names = balanceVarLabels,
                                #  var.order = "unadjusted", 
                                colors = c("black","grey"), 
                                abs = FALSE,
                                shapes = c(17,19))+
  theme_classic()+
  theme(axis.text=element_text(size=5))+
  ggtitle("States Not Expressing Public Criticism")+
  xlim(-1,1)


BacklashBalance$data$period <- "Expressing Public\nCriticism"
NonBacklashBalance$data$period <- "Not Expressing Public\nCriticism"

balanceData <- bind_rows(BacklashBalance$data, 
                         NonBacklashBalance$data) %>%
  mutate(group = paste(period, Sample)) %>%
  group_by(var) %>%
  mutate(PlotOrdering = mean(ifelse(group == "Public Criticism Unadjusted", stat, NA), na.rm = TRUE)) %>%
  as.data.frame()


pdf("figure3.pdf", width = 6.5, height = 6.5 )
print(ggplot(balanceData)+
        geom_jitter(aes(x = stat, y= reorder(var, X= PlotOrdering), shape = group ), height = 0.1, width = 0)+
        theme_classic()+
        ylab("") +
        xlab("Standardized mean differences")+
        geom_vline(aes(xintercept = 0), linetype = "dashed", color = "darkgrey")+
        theme(legend.title=element_blank()))
dev.off()

mod1 <- lm(ViolationRuling ~ 
             BacklashTimevarying * ConDem +
             post2010 +
             GrandChamberJudgment + 
             proportionCasesClosed +
             article2dummy + 
             article3dummy + article5dummy +article6dummy+ article8dummy + 
             article10dummy+article14dummy + protocol1_1dummy+
             Importance1 + Importance2 + medianJudge + 
             lawyerPreviousViolationJudgments +
             refugee + prisoner,
           data = matchedCem1,
           weights = matchedCem1$weight)

summary(mod1)
mod1.tidy <- tidy(mod1)
mod1.tidy$description <- "Model 1:  Change in probability of violation \n associated with public criticism of the Court \n  Consolidated democracies \n vs.  all other other states \n coarsened exact matching"

Mode1Figure <- mod1.tidy %>% 
  mutate(term = factor(term, levels = c("(Intercept)",
                                        "refugee",
                                        "prisoner", 
                                        "protocol1_1dummy", 
                                        "article14dummy",
                                        "article10dummy", 
                                        "article8dummy",
                                        "article6dummy",
                                        "article5dummy", 
                                        "article3dummy",
                                        "article2dummy",
                                        "Importance2",          
                                        "Importance1",
                                        "GrandChamberJudgment", 
                                        "proportionCasesClosed",
                                        "lawyerPreviousViolationJudgments",
                                        "medianJudge",
                                        "post2010",
                                        "BacklashTimevarying:ConDem", 
                                        "BacklashTimevarying",
                                        "ConDem"),
                       labels = c("Intercept",
                                  "Refugee or asylum seeker applicant",
                                  "Prisoner applicant", 
                                  "Article 1 of Protocol 1 case", 
                                  "Article 14 case",
                                  "Article 10 case", 
                                  "Article 8 case",
                                  "Article 6 case",
                                  "Article 5 case", 
                                  "Article 3 case",
                                  "Article 2 case",
                                  "Importance level 2",          
                                  "Importance level 1",
                                  "Grand Chamber judgment", 
                                  "Proportion of compliance with previous cases",
                                  "Previous experience of legal council",
                                  "Ideal point of median judge on panel",
                                  "Post-2010 judgment",
                                  "Consolidated democracy * Public critic", 
                                  "Public critic",
                                  "Consolidated democracy"),
                       ordered = TRUE),
         varType = ifelse(term == "Consolidated democracy * Public critic" |
                            term ==  "Consolidated democracy" |
                            term == "Public critic", "1Substantive", "2Control")) 

pdf("figure4.pdf", width =6.5, height = 6.5)
print(Mode1Figure  %>% 
        ggplot(aes(y = term,
                   x = estimate, 
                   color = varType,
                   xmin = estimate - 1.96 * abs(std.error), 
                   xmax = estimate + 1.96 * abs(std.error)))+
        geom_errorbarh(height = .1)+
        geom_point()+
        scale_color_grey() +
        geom_vline(aes(xintercept = 0), linetype = "dashed") +
        xlim(-1,1)+
        theme_classic()+
        theme(legend.position = "none") +
        theme(axis.text.y =element_text(face=ifelse(Mode1Figure$varType[order(Mode1Figure$term)]== "1Substantive","bold","plain")))+
        ylab("") +
        xlab("Coefficient"))
dev.off()


mod2 <- lm(shareViolationRulings ~ 
             BacklashTimevarying * ConDem +
             post2010 +
             GrandChamberJudgment + 
             proportionCasesClosed +
             article2dummy + 
             article3dummy + article5dummy +article6dummy+ article8dummy + 
             article10dummy+article14dummy + protocol1_1dummy+
             Importance1 + Importance2 + medianJudge + 
             lawyerPreviousViolationJudgments +
             refugee + prisoner,
           data = matchedCem1,
           weights = matchedCem1$weight)

summary(mod2)
mod2.tidy <- tidy(mod2)
mod2.tidy$description <- "Model 2:  Change in share of violation findings \n associated with public criticism of the Court \n  Consolidated democracies \n vs.  all other other states \n coarsened exact matching"


## Model 3 (Genetic matching, Consolidated democracies vs. other states)
## NB: the genetic matching takes a while to run. We therefore comment out this part of the code 
## and supply the results from the call to matchit() as a separate file
# m.genetic <- matchit(ConDem ~
#                        year + GrandChamberJudgment +
#                        article2dummy + article3dummy +
#                        article5dummy +  article6dummy +
#                        article8dummy + article10dummy +
#                        article14dummy + protocol1_1dummy +
#                        Importance1 +Importance2 +  medianJudge +
#                        proportionCasesClosed+
#                        refugee + prisoner,
#                      data = matchingData1,
#                      method="genetic",
#                      unif.seed = 336890,
#                      caliper = 1,
#                      wait.generations= 10,
#                      ratio = 1,
#                      pop.size = 1000,
#                      replace = TRUE,
#                      ties = TRUE)
# save(m.genetic,
#      file = "GeneticMatches16January2020.RData")
load("GeneticMatches16January2020.RData")

matchingData1$weight  <- m.genetic$weights
genetic.matches <- matchingData1[which(matchingData1$weight != 0),]

mod3 <- lm(ViolationRuling ~ 
             BacklashTimevarying * ConDem + post2010 + 
             GrandChamberJudgment + proportionCasesClosed + article2dummy + 
             article3dummy + article5dummy + article6dummy + article8dummy + 
             article10dummy + article14dummy + protocol1_1dummy + Importance1 + 
             Importance2 + medianJudge + lawyerPreviousViolationJudgments + 
             refugee + prisoner, 
           data = genetic.matches, weights = weight)
summary(mod3)

mod3.tidy <- tidy(mod3)
mod3.tidy$description <- "Model 3:  Change in probability of violation \n associated with public criticism of the Court \n  Consolidated democracies \n vs.  all other other states \n genetic matching"


matchDataEU <- matchingData1[which(matchingData1$ConDem == 1 | matchingData1$EUmember2018 == 1),]
m.cem.eu <- matchit(ConDem ~ year + GrandChamberJudgment +proportionCasesClosed+ article2dummy + article3dummy + article5dummy +article6dummy+ article8dummy + 
                      article10dummy+article14dummy + protocol1_1dummy+ Importance1 + Importance2 + medianJudge +
                      refugee+prisoner, data = matchDataEU , method="cem", 
                    cutpoints = cutPoints)

matchDataEU$weight <- m.cem.eu$weights
matchDataEU <- matchDataEU[which(matchDataEU$weight != 0),]
mod4 <- lm(ViolationRuling ~ 
             BacklashTimevarying * ConDem +
             post2010 +
             GrandChamberJudgment + 
             proportionCasesClosed +
             article2dummy + 
             article3dummy + article5dummy +article6dummy+ article8dummy + 
             article10dummy+article14dummy + protocol1_1dummy+ Importance1 + Importance2 + medianJudge + lawyerPreviousViolationJudgments +
             refugee + prisoner, 
           data = matchDataEU,
           weights = weight)

mod4.tidy <- tidy(mod4)
mod4.tidy$description <- "Model 4: Change in probability of violation \n after public criticism of the Court \n  Consolidated democracies vs.\nother EU members/candidates \n coarsened exact matching"

matchingData2 <- filter(matchingData1, 
                        ConDem == 1,
                        year > 2009)

m.cemAfterBacklash <- matchit(BacklashTimevarying ~ year +
                                GrandChamberJudgment +
                                proportionCasesClosed +
                                article2dummy + article3dummy +
                                article5dummy + article6dummy+
                                article8dummy + article10dummy +
                                article14dummy + protocol1_1dummy +
                                Importance1 + Importance2 +
                                medianJudge +
                                lawyerPreviousViolationJudgments +
                                refugee + prisoner,
                              data = matchingData2, 
                              method="cem", cutpoints = list(medianJudge = 4, 
                                                             proportionCasesClosed = c(0,0.25,0.5, 0.75,1), 
                                                             lawyerPreviousViolationJudgments = c(0,1, 10), 
                                                             year = 5))


matchingData2$weight <- m.cemAfterBacklash$weights
matchingData2 <- filter(matchingData2, weight != 0)

mod5 <- lm(ViolationRuling ~ 
             BacklashTimevarying  +
             GrandChamberJudgment +
             proportionCasesClosed +
             article2dummy +
             article3dummy + article5dummy +article6dummy+ article8dummy +
             article10dummy+article14dummy + protocol1_1dummy+
             Importance1 + Importance2 + medianJudge +
             lawyerPreviousViolationJudgments +
             refugee + prisoner,
           data = matchingData2,
           weights = matchingData2$weight)
summary(mod5)

mod5.tidy <- tidy(mod5)
mod5.tidy$description <- "Model 5: Change in probability of violation\n associated with public criticism\n Post-2010 judgments against\nconsolidated democracies only\n coarsened exact matching"

### Model 6 (coarsened exact matching: UK vs. other consolidated democracies)

matchingDataUK <- matchingData1[which(matchingData1$ConDem == 1),]


m.cem.uk <- matchit(UK ~ year + GrandChamberJudgment + proportionCasesClosed + article2dummy + article3dummy + article5dummy +article6dummy+ article8dummy + 
                      article10dummy+article14dummy + protocol1_1dummy+ Importance1 + Importance2 + medianJudge +refugee+prisoner, 
                    data = matchingDataUK, method="cem", cutpoints = cutPoints)

summary(m.cem.uk)
matchingDataUK$weight <- matchingDataUK$weight


mod6 <- lm(ViolationRuling ~ 
             post2005 * UK + post2010 +
             GrandChamberJudgment + 
             proportionCasesClosed +
             article2dummy + 
             article3dummy + article5dummy +article6dummy+ article8dummy + 
             article10dummy+article14dummy + protocol1_1dummy+ 
             Importance1 + Importance2 + medianJudge + lawyerPreviousViolationJudgments+
             refugee+ prisoner, 
           data = matchingDataUK,
           weights = weight)
summary(mod6)
mod6.tidy <- tidy(mod6)
mod6.tidy$description <- "Model 6: Change in probability of violation ruling after 2005 \n  United Kingdom vs. \n other consolidated democracies \n coarsened exact matching"


matchDataNonUK <- matchingData1[which(matchingData1$UK == 0),]

m.cem.nonUK <- matchit(ConDem ~ year + GrandChamberJudgment + proportionCasesClosed + article2dummy + article3dummy + article5dummy +article6dummy+ article8dummy + 
                         article10dummy+article14dummy + protocol1_1dummy+ Importance1 + Importance2 + medianJudge +
                         refugee+prisoner, data = matchDataNonUK, method="cem", 
                       cutpoints = cutPoints)
summary(m.cem.nonUK)
matchDataNonUK$weight <- m.cem.nonUK$weights
matchDataNonUK <- matchDataNonUK[which(matchDataNonUK$weight != 0),]

mod7 <- lm(ViolationRuling ~ 
             BacklashTimevarying * ConDem + post2010+ 
             GrandChamberJudgment + 
             proportionCasesClosed +
             article2dummy + 
             article3dummy + article5dummy +article6dummy+ article8dummy + 
             article10dummy+article14dummy + protocol1_1dummy+ Importance1 + Importance2+ medianJudge +lawyerPreviousViolationJudgments +
             refugee + prisoner, 
           data = matchDataNonUK,
           weights = weight)
summary(mod7)

mod7.tidy <- tidy(mod7)
mod7.tidy$description <- "Model 7: Change in probability of violation ruling\nassociated with public criticism \n  Consolidated democracies vs. other states,\nUnited Kingdom excluded \n coarsened exact matching"



#### Coef plot ####
differences <- rbind(mod1.tidy[nrow(mod1.tidy),], 
                     mod2.tidy[nrow(mod2.tidy),], 
                     mod3.tidy[nrow(mod3.tidy),], 
                     mod4.tidy[nrow(mod4.tidy),], 
                     mod5.tidy[2,],
                     mod6.tidy[nrow(mod6.tidy),],
                     mod7.tidy[nrow(mod7.tidy),])

differences$conf.low <- differences$estimate - 1.96*abs(differences$std.error)
differences$conf.high <- differences$estimate + 1.96*abs(differences$std.error)

differences$description <- as.factor(differences$description)


pdf("Figure5.pdf",  width = 6.5, height = 6.5)
print(ggplot(differences, 
             aes(x=estimate, 
                 y= description, xmin=conf.low, xmax=conf.high)) + 
        geom_point()+ geom_errorbarh(height=.05) + 
        theme_classic()+geom_vline(xintercept = 0,linetype=3) +
        ylab("")+
        xlab("Estimated change in violation rate")+
        xlim(-.6, .4))
dev.off()

##################################################################
### Additional tables and figures for the online appendix      ###
##################################################################

## Summary statistics: 
vars <- matchingData1 %>% 
  dplyr::select(-weight, -ConsolidatedChar, -cowcode, - year) %>% 
  dplyr::select(ViolationRuling,shareViolationRulings,ConDem:prisoner  )
stargazer(vars,
          type = "latex",
          title = "Summary statistics",  
          style = "apsr",  
          label = "tab:descriptives",
          summary = TRUE,  
          header = FALSE,
          covariate.labels = c("Violation ruling",  
                               "Share of violation rulings", 
                               "Consolidated democracy",
                               "Public critic", 
                               "Post-2005 judgment", 
                               "Post-2010 judgment", 
                               "United Kingdom", 
                               "Proportion of judgments complied with in previous year", 
                               "Grand Chamber judgment",
                               "Article 2 case", 
                               "Article 3 case",  
                               "Article 5 case",  
                               "Article 6 case",
                               "Article 8 case", 
                               "Article 10 case",  
                               "Article 14 case",  
                               "Protocol 1, article 1 case",  
                               "Importance level 1",  
                               "Importance level 2", 
                               "Ideal point of median judge on panel",  
                               "Previous experience of legal council",
                               "EU Member or Candidate", 
                               "Refugee or asylum seeker applicant",  
                               "Prisoner applicant"), 
          font.size = "scriptsize")

### Balance assessments for all models: 
balanceVarLabels <- data.frame(old = colnames(m.cem1$X),
                               new = c("Year of judgment",  "Grand Chamber judgment",
                                       "Proportion of judgments complied with in previous year",
                                       "Article 2 case","Article 3 case",
                                       "Article 5 case", "Article 6 case",
                                       "Article 8 case", "Article 10 case",
                                       "Article 14 case", "Protocol 1, article 1 case",
                                       "Importance level 1", "Importance level 2",
                                       "Ideal point, median judge",
                                       "Previous experience of applicant's lawyer",
                                       "Refugee or immigrant applicant", "Prisoner applicant"))

balance1 <- love.plot(bal.tab(m.cem1, binary = "std", continuous = "std"),
                      var.names = balanceVarLabels,
                      drop.distance = TRUE,
                      var.order = "unadjusted",
                      colors = c("black","grey"),
                      abs = FALSE,
                      shapes = c(17,19))+
  theme_classic()+
  theme(axis.text=element_text(size=5))+
  ggtitle("Models 1 and 2")+
  xlim(-1,1)


balance2 <- love.plot(bal.tab(m.genetic, binary = "std", continuous = "std"),
                      var.names = balanceVarLabels,
                      drop.distance = TRUE,
                      var.order = "unadjusted",
                      colors = c("black","grey"),
                      abs = FALSE,
                      shapes = c(17,19))+
  theme_classic()+
  theme(axis.text=element_text(size=5))+
  ggtitle("Model 3")+
  xlim(-1,1)


balance3 <- love.plot(bal.tab(m.cem.eu, binary = "std", continuous = "std"),
                      var.names = balanceVarLabels,
                      drop.distance = TRUE,
                      var.order = "unadjusted",
                      colors = c("black","grey"),
                      abs = FALSE,
                      shapes = c(17,19))+
  theme_classic()+
  theme(axis.text=element_text(size=5))+
  ggtitle("Model 4")+
  xlim(-1,1)

balance4 <- love.plot(bal.tab(m.cemAfterBacklash, binary = "std", continuous = "std"),
                      var.names = balanceVarLabels,
                      drop.distance = TRUE,
                      var.order = "unadjusted",
                      colors = c("black","grey"),
                      abs = FALSE,
                      shapes = c(17,19))+
  theme_classic()+
  theme(axis.text=element_text(size=5))+
  ggtitle("Model 5")+
  xlim(-1,1)



balanceVarLabels <- rbind(balanceVarLabels, data.frame(old = "UK", new = "United Kingdom"))

balance5 <- love.plot(bal.tab(m.cem.uk, binary = "std", continuous = "std"),
                      var.names = balanceVarLabels,
                      drop.distance = TRUE,
                      var.order = "unadjusted",
                      colors = c("black","grey"),
                      abs = FALSE,
                      shapes = c(17,19))+
  theme_classic()+
  theme(axis.text=element_text(size=5))+
  ggtitle("Model 6")+
  xlim(-1,1)


balance6 <- love.plot(bal.tab(m.cem.nonUK, binary = "std", continuous = "std"),
                      var.names = balanceVarLabels,
                      drop.distance = TRUE,
                      var.order = "unadjusted",
                      colors = c("black","grey"),
                      abs = FALSE,
                      shapes = c(17,19))+
  theme_classic()+
  theme(axis.text=element_text(size=5))+
  ggtitle("Model 7")+
  xlim(-1,1)



ggarrange(balance1, balance2,
          balance3, balance4,
          balance5, balance6,
          ncol = 2, nrow = 3)

## Regression table 
lpm.models <- list(mod1, mod2, mod3, mod4, mod5, mod6, mod7)
coef.mapping <-  list(ConDem = "Consolidated democracy",  
                      BacklashTimevarying = "Public critic", 
                      `BacklashTimevarying:ConDem` = "Consolidated democracy * public critic",
                      UK  = "United Kingdom", 
                      post2005 = "Post-2005 judgment",  
                      `post2005:UK` = "United Kingdom * Post-2005 judgment",
                      post2010 = "Post-2010 judgment", 
                      GrandChamberJudgment = "Grand Chamber judgment",
                      proportionCasesClosed = "Proportion of judgments complied with in previous year", 
                      article2dummy = "Article 2 case", 
                      article3dummy = "Article 3 case", 
                      article5dummy = "Article 5 case", 
                      article6dummy = "Article 6 case", 
                      article8dummy = "Article 8 case", 
                      article10dummy = "Article 10 case" , 
                      article14dummy = "Article 14 case", 
                      protocol1_1dummy = "Protocol 1, article 1 case", 
                      Importance1 = "Importance level 1",  
                      Importance2 = "Importance level 2",  
                      medianJudge = "Ideal point of median judge",  
                      lawyerPreviousViolationJudgments  = "Previous experience of legal council", 
                      prisoner = "Prisoner applicant", 
                      refugee = "Refugee/asylum seeker applicant",
                      `(Intercept)` = "Intercept")



screenreg(l = lpm.models,
          fontsize = "footnotesize", 
          caption.above = TRUE, 
          label = "tab:regressions",
          caption = "Full regression results for Figures 4 and 5 of the main article", 
          no.margin = TRUE,
          custom.coef.map = coef.mapping)


###################################################################
## The following models are reported only in the online appendix:##
###################################################################

### Alternative logit models: 
toLogit <- function(x){
  lpm.weights <- x$weights 
  lpm.formula <- formula(x)
  attributes(lpm.formula)$.Environment <- environment() 
  lpm.data <- x$model
  
  logit <- glm(formula =  lpm.formula,
               weights = lpm.weights,
               data = lpm.data, 
               family = binomial(link = "logit")) 
} 

logit.models <- lapply(lpm.models, 
                       FUN = toLogit) 

texreg(l = logit.models, 
       fontsize = "footnotesize", 
       caption.above = TRUE, 
       label = "tab:logits", 
       caption = "Logit model results for models reported in Figures 4 and 5 of the main article", 
       custom.model.names = paste("Logit", 1:length(logit.models)), 
       no.margin = TRUE, 
       custom.coef.map = coef.mapping)  


### With importance level 3 for ####
withImportanceLevel3 <- filter(judgments, 
                               violationOrNoViolation == 1, # We only include judgments that decide on whether a violation has occured
                               Judgment.Date >= as.Date("1998-11-01", format = "%Y-%m-%d")) %>% 
  mutate(Importance3 = ifelse(Importance.Level == "3", 1,0)) %>% 
  select(ConDem, ConsolidatedChar, year, 
         BacklashTimevarying, post2005, post2010, UK,
         proportionCasesClosed, shareViolationRulings, GrandChamberJudgment, 
         article2dummy, article3dummy, article5dummy,
         article6dummy, article8dummy, article10dummy, article14dummy, 
         protocol1_1dummy, Importance1, Importance2,Importance3,  ViolationRuling,  
         medianJudge, lawyerPreviousViolationJudgments,
         EUmember2018, refugee, prisoner, cowcode) %>% 
  na.omit()

withImportanceLevel3_1 <- lm(ViolationRuling ~ 
             BacklashTimevarying * ConDem +
             post2010 +
             GrandChamberJudgment + 
             proportionCasesClosed +
             article2dummy + 
             article3dummy + article5dummy +article6dummy+ article8dummy + 
             article10dummy+article14dummy + protocol1_1dummy+
             Importance1 + Importance2 + Importance3 + medianJudge + 
             lawyerPreviousViolationJudgments +
             refugee + prisoner,
           data = withImportanceLevel3)

withImportanceLevel3$weight <-  matchit(formula = ConDem ~ year + GrandChamberJudgment + proportionCasesClosed + 
          article2dummy + article3dummy + article5dummy + article6dummy + 
          article8dummy + article10dummy + article14dummy + protocol1_1dummy + 
          Importance1 + Importance2 + Importance3 + medianJudge + lawyerPreviousViolationJudgments + 
          refugee + prisoner, data = withImportanceLevel3, method = "cem", 
        cutpoints = cutPoints)$weights


withImportanceLevel3_2 <- lm(ViolationRuling ~ 
                               BacklashTimevarying * ConDem +
                               post2010 +
                               GrandChamberJudgment + 
                               proportionCasesClosed +
                               article2dummy + 
                               article3dummy + article5dummy +article6dummy+ article8dummy + 
                               article10dummy+article14dummy + protocol1_1dummy+
                               Importance1 + Importance2 + Importance3 + medianJudge + 
                               lawyerPreviousViolationJudgments +
                               refugee + prisoner,
                             data = filter(withImportanceLevel3, weight != 0), 
                             weight = filter(withImportanceLevel3, weight != 0)$weight)

summary(withImportanceLevel3_2)
coef.mapping <-  list(ConDem = "Consolidated democracy",  
                      BacklashTimevarying = "Public critic", 
                      `BacklashTimevarying:ConDem` = "Consolidated democracy * public critic",
                      proportionCasesClosed = "Proportion of judgments complied with in previous year", 
                      `BacklashTimevarying:proportionCasesClosed` = "Public critic *
                      Proportion of judgments complied with in previous year",
                      `ConDem:proportionCasesClosed` = "Consolidated democracy * 
                      Proportion of judgments complied with in previous year",
                      `BacklashTimevarying:ConDem:proportionCasesClosed`= "Consolidated democracy * public critic * 
                      Proportion of judgments complied with in previous year",
                      GrandChamberJudgment = "Grand Chamber judgment",  
                      article2dummy = "Article 2 case", 
                      article3dummy = "Article 3 case", 
                      article5dummy = "Article 5 case", 
                      article6dummy = "Article 6 case", 
                      article8dummy = "Article 8 case", 
                      article10dummy = "Article 10 case" , 
                      article14dummy = "Article 14 case", 
                      protocol1_1dummy = "Protocol 1, article 1 case", 
                      Importance1 = "Importance level 1",  
                      Importance2 = "Importance level 2",  
                      Importance3 = "Importance level 3",
                      medianJudge = "Ideal point of median judge",  
                      lawyerPreviousViolationJudgments  = "Previous experience of applicant's lawyer", 
                      prisoner = "Prisoner applicant",  
                      refugee = "Refugee/asylum seeker applicant") 
  
  
texreg(l = list(withImportanceLevel3_1,
  withImportanceLevel3_2), 
       fontsize = "footnotesize", 
       caption.above = TRUE, 
       label = "tab:regressions", 
       caption = "Models estimated on all judgments irrespective of importance level, before and after matching", 
       no.margin = TRUE, 
       custom.coef.map = coef.mapping)  


#### Three-way interaction with previous compliance performance. 
threewayinteraction <- lm(formula = ViolationRuling ~ BacklashTimevarying * ConDem * proportionCasesClosed+ 
     post2010 + GrandChamberJudgment + proportionCasesClosed + 
     article2dummy + article3dummy + article5dummy + article6dummy + 
     article8dummy + article10dummy + article14dummy + protocol1_1dummy + 
     Importance1 + Importance2 + medianJudge + lawyerPreviousViolationJudgments + 
     refugee + prisoner, data = matchingData1)
summary(threewayinteraction)

#### Without Norway, Denmark, Sweden
withoutScandinavia <- lm(formula = ViolationRuling ~ BacklashTimevarying * ConDem + 
                           post2010 + GrandChamberJudgment + proportionCasesClosed + 
                           article2dummy + article3dummy + article5dummy + article6dummy + 
                           article8dummy + article10dummy + article14dummy + protocol1_1dummy + 
                           Importance1 + Importance2 + medianJudge + lawyerPreviousViolationJudgments + 
                           refugee + prisoner, data = filter(matchingData1, respondent_1 != "Sweden", 
                                                             respondent_1 != "Norway", 
                                                             respondent_1 != "Denmark"))

texreg(l = list(threewayinteraction,
                withoutScandinavia), 
       fontsize = "footnotesize", 
       caption.above = TRUE, 
       label = "tab:complianceMods", 
       caption = "Three way interaction between public criticism, consolidated democracy, and previous compliance performance, and model estimated after excluding Denmark, Sweden, Norway identified by von Stated (2018) as exhibiting near-perfect compliance", 
       no.margin = TRUE, 
       custom.coef.map = coef.mapping) 
